<template>
  <el-dialog :visible.sync="dialogTableVisible" :append-to-body="true" :lock-scroll="false" :title="isEdit?'Edit':'Create'" @close="handleCloseDetailDialog">
    <el-form ref="postForm" :model="postForm" :rules="rules" label-position="left" label-width="70px" style="margin-left:50px;">
      <el-form-item :label="$t('permissionfiles.pid')" prop="pid" >
        <el-col :span="20">
          <el-cascader
            :options="permissionList"
            :props="permissionProps"
            :show-all-levels="false"
            :clearable="true"
            :change-on-select="true"
            v-model="selectedPermissionOptions"
          />
        </el-col>
      </el-form-item>

      <el-form-item :label="$t('permissionfiles.enname')" prop="enname" required>
        <el-col :span="20">
          <el-input v-model="postForm.enname" :label="$t('permissionfiles.enname')"/>
        </el-col>
      </el-form-item>
      <el-form-item :label="$t('permissionfiles.name')" prop="name" required>
        <el-col :span="20">
          <el-input v-model="postForm.name" :label="$t('permissionfiles.name')"/>
        </el-col>
      </el-form-item>
      <el-form-item :label="$t('permissionfiles.descript')" prop="descript" >
        <el-col :span="20">
          <el-input v-model="postForm.descript" :label="$t('permissionfiles.descript')" type="textarea"/>
        </el-col>
      </el-form-item>
      <el-form-item :label="$t('permissionfiles.ptype')" prop="ptype" >
        <el-col :span="20">
          <el-radio-group v-model="postForm.ptype">
            <el-radio :label="2">Manage</el-radio>
            <el-radio :label="1">List</el-radio>
            <el-radio :label="0">Operation</el-radio>
          </el-radio-group>
        </el-col>
      </el-form-item>

    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCloseDetailDialog">{{ $t('table.cancel') }}</el-button>
      <el-button type="primary" @click="submitForm('postForm')">{{ $t('table.submit') }}</el-button>
    </div>

  </el-dialog>
</template>
<script>

import permissionApi from '@/api/system/permission'
import ScrollPane from '../../../../../components/ScrollPane/index'

const defaultForm = {
  pid: 0,
  enname: '',
  name: '',
  descript: '',
  ptype: 2
}

export default {
  name: 'Detail',
  components: { ScrollPane },
  data() {
    return {
      postForm: Object.assign({}, defaultForm),
      rules: {
        name: [
          { required: true, message: 'name is required', trigger: 'blur' }
        ],
        email: [
          { required: true, message: 'email is required', trigger: 'blur' }
        ],
        password: [
          { required: true, message: 'password is required', trigger: 'blur' }
        ]
      },
      isEdit: false,
      areaList: [],
      imageUrl: '',
      recordid: 0,
      dialogTableVisible: false,
      permissionList: [],
      permissionProps: {
        label: 'name',
        value: 'id',
        children: '_child'
      },
      selectedPermissionOptions: []
    }
  },
  computed: {
    lang() {
      return this.$store.getters.language
    }
  },
  mounted() {

  },
  methods: {
    showRecord(id) {
      permissionApi.showRecord(id).then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.postForm = serviceResponse.data.record
          this.permissionList = serviceResponse.data.permissionList
          this.selectedPermissionOptions = serviceResponse.data.selectPermission
        }
      }).catch(err => {
        console.log(err)
      })
    },

    handleCreateRecord() {
      permissionApi.createRecord().then(response => {
        const serviceResponse = response.data
        if (serviceResponse.code === 0) {
          this.permissionList = serviceResponse.data.permissionList
        }
      }).catch(err => {
        console.log(err)
      })
    },

    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const selectLength = this.selectedPermissionOptions.length
          if (selectLength > 0) {
            this.postForm.pid = this.selectedPermissionOptions[selectLength - 1]
          }
          if (this.editId > 0) {
            permissionApi.updateRecord(this.editId, this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          } else {
            permissionApi.storeRecord(this.postForm).then(response => {
              const serviceResponse = response.data
              if (serviceResponse.code === 0) {
                this.$message({
                  type: 'success',
                  message: serviceResponse.msg
                })
                this.handleCloseDetailDialog()
              } else {
                this.$message({
                  type: 'error',
                  message: serviceResponse.msg
                })
              }
            })
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm(postForm) {
      this.$refs[postForm].resetFields()
    },

    handleImagesSuccess(res, file) {
      if (res.code === 0) {
        this.postForm.ad_image = res.data.paths[0].path
        const imgpath = res.data.imgpath
        this.imageUrl = imgpath + res.data.paths[0].path
      } else {
        alert(res.data.msg)
      }
    },
    handShowDetailDialog(id) {
      this.selectedPermissionOptions = []
      this.permissionList = []

      if (id > 0) {
        this.isEdit = true
        this.editId = id
        this.showRecord(id)
      } else {
        this.isEdit = false
        this.handleCreateRecord()
        this.postForm = Object.assign({}, defaultForm)
      }
      this.dialogTableVisible = true
    },
    handleCloseDetailDialog() {
      this.resetForm('postForm')
      this.editId = 0
      this.$emit('indexRecord')
      this.dialogTableVisible = false
    }
  }

}
</script>
